﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;

namespace Ghy.Soft.Data
{
    public class BaseDao<T>
    {
        public T FindById(int id)
        {
            using (ISession session = FluentFactory.GetSession())
            {
                return session.Load<T>(id);
            }
        }

        public int Save(T t)
        {
            using (ISession session = FluentFactory.GetSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    int id = (int)session.Save(t);
                    transaction.Commit();
                    return id;
                }
            }
        }

        public void Update(T t, int id)
        {
            using (ISession session = FluentFactory.GetSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(t, id);
                    transaction.Commit();
                }
            }

        }
    }
}
